﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {
        /// <summary>
        /// FS:LabelType=Other标签处理函数
        /// </summary>
        /// <returns></returns>
        public string ProductType()
        {
            string mystyle = this.Label_Inserted;
            string str_ShowMode = this.GetParamValue("YX:ShowMode");
            string str_ProGroupRows = this.GetParamValue("YX:ProGroupRows");
            if (str_ProGroupRows == null)
            {
                str_ProGroupRows = "4";
            }
            string str_ProGroupColumns = this.GetParamValue("YX:ProGroupColumns");
            if (str_ProGroupColumns == null)
            {
                str_ProGroupColumns = "1";
            }
            string str_ProValueRows = this.GetParamValue("YX:ProValueRows");
            if (str_ProValueRows == null)
            {
                str_ProValueRows = "1";
            }
            string str_ProValueColumns = this.GetParamValue("YX:ProValueColumns");
            if (str_ProValueColumns == null)
            {
                str_ProValueColumns = "8";
            }
            string str_PropertyGroupState = this.GetParamValue("YX:PropertyGroupState");
            if (str_PropertyGroupState == null)
            {
                str_PropertyGroupState = "true";
            }
            string str_PropertyValueState = this.GetParamValue("YX:PropertyValueState");
            if (str_PropertyValueState == null)
            {
                str_PropertyValueState = "true";
            }
            string str_ProductClassId = this.GetParamValue("YX:ProductClassId");
            if (str_ProductClassId == null)
            {
                str_ProductClassId = "0";
            }
            int proGroupRows = Convert.ToInt32(str_ProGroupRows) * Convert.ToInt32(str_ProGroupColumns);
            StringBuilder shtml = new StringBuilder();
            if (mystyle != string.Empty)
            {
                int rowsCount = 0;
                int productClassId = 0;
                if (str_ShowMode == "1")
                {
                    productClassId = ChangeHope.WebPage.PageRequest.GetQueryInt("q_cid");
                }
                else if (str_ShowMode == "2")
                {
                    productClassId = Convert.ToInt32(str_ProductClassId);
                }
                
                YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
                
                DataTable dt = bll.GetProductTypeBrandAndProperty(productClassId);
                if (dt.Rows.Count > 0)
                {
                    string strProperty = ChangeHope.WebPage.PageRequest.GetQueryString("q_property");
                    string brandId=ChangeHope.WebPage.PageRequest.GetQueryString("q_productbrand");
                    string url = "";
                    if (dt.Rows[0]["RelevanceBrand"].ToString() != "")
                    {
                        YXShop.BLL.Product.ProductBrand brandBll = new YXShop.BLL.Product.ProductBrand();
                        List<YXShop.Model.Product.ProductBrand> brandList = brandBll.GetDTList(dt.Rows[0]["RelevanceBrand"].ToString());
                        if (brandList.Count > 0)
                        {
                            
                            string strContent = mystyle;
                            string strBrandList = "";
                            foreach (YXShop.Model.Product.ProductBrand brand in brandList)
                            {
                                if (string.IsNullOrEmpty(strProperty))
                                {
                                    url = Common.UrlStr("productlist", "q_cid=" + productClassId + ";q_productbrand=" + brand.ID + "", "product/");
                                }
                                else
                                {
                                    url = Common.UrlStr("productlist", "q_cid=" + productClassId + ";q_productbrand=" + brand.ID + ";q_property=" + strProperty + "", "product/");
                                }
                                if (brand.Name != "")
                                {
                                    string style = "property_UnSelected";
                                    if (brandId == brand.ID.ToString())
                                    {
                                        style = "property_Selected";
                                    }
                                    strBrandList += "<span class='" + style + "'><a href='" + url + "'>" + brand.Name + "</a></span>";
                                }
                            }
                            if (strContent.Contains("[#Name]"))
                            {
                                strContent = strContent.Replace("[#Name]", "品牌");
                            }
                            if (strContent.Contains("[#Whole]"))
                            {
                                string style = "property_UnSelected";
                                string urls = Common.UrlStr("productlist", "q_cid=" + productClassId + "", "product/");
                                if (!string.IsNullOrEmpty(strProperty.Trim()))
                                {
                                    urls = Common.UrlStr("productlist", "q_cid=" + productClassId + ";q_property=" + strProperty + "", "product/");
                                }
                                if (string.IsNullOrEmpty(brandId.Trim()))
                                {
                                    style = "property_Selected";
                                }
                                strContent = strContent.Replace("[#Whole]", "<span class='" + style + "'><a href='" + urls + "'>全部</a></span>");
                            }
                            if (strContent.Contains("[#Value]"))
                            {
                                strContent = strContent.Replace("[#Value]", strBrandList);
                            }
                            rowsCount++;
                            shtml.Append(strContent);
                        }
                    }
                   
                    List<YXShop.Model.Product.ProductTypeProperty> propertyList = bll.GetProductPropertyAllByWhere(Convert.ToInt32(dt.Rows[0]["Id"].ToString()), "");
                    if (propertyList.Count > 0)
                    {
                        string propertyUrl = string.Empty;
                        
                        foreach (YXShop.Model.Product.ProductTypeProperty property in propertyList)
                        {
                            string strContent = mystyle;
                            if ((property.PropertyValue != "" && !string.IsNullOrEmpty(property.PropertyName) && property.PropertyClass == 3) || property.PropertyType == 1 || property.PropertyType == 2 || property.PropertyType == 3)
                            {
                                string strPropertyValueList = "";
                                string fullPropertyInfo = "";
                                string[] strPropertyValue = property.PropertyValue.Replace("\r\n", "\r").Split('\r');
                                foreach (string pv in strPropertyValue)
                                {
                                    string strPropertyVa = "";
                                   
                                    if (!string.IsNullOrEmpty(pv.Trim()))
                                    {
                                        string style = "property_UnSelected";
                                        string pv_parameter = property.ID + "_" + pv;
                                        if (!string.IsNullOrEmpty(strProperty.Trim()))
                                        {
                                            bool isExts = false;
                                            string[] propertyArr = strProperty.Split(',');
                                            for (int i = 0; i < propertyArr.Length; i++)
                                            {
                                                string[] propertySon = propertyArr[i].Split('_');
                                                if (propertyArr[i] == pv_parameter)
                                                {
                                                    if (strProperty.IndexOf(",") >0)
                                                    {
                                                         fullPropertyInfo = strProperty.Replace(propertyArr[i] + ",", "").Replace("," + propertyArr[i], "");
                                                    }
                                                    else
                                                    {
                                                        fullPropertyInfo = strProperty.Replace(propertyArr[i], "");
                                                    }
                                                    strPropertyVa = strProperty.Replace(propertyArr[i], pv_parameter);
                                                    style = "property_Selected";
                                                    isExts = true;
                                                }
                                                else
                                                {
                                                    if (propertySon[0] == property.ID.ToString())
                                                    {
                                                        strPropertyVa = strProperty.Replace(propertyArr[i], pv_parameter);
                                                        isExts = true;
                                                    }
                                                }
                                            }

                                            if (!isExts)
                                            {
                                                fullPropertyInfo = strProperty;
                                                strPropertyVa = strProperty + "," + pv_parameter;
                                            }
                                        }
                                        else
                                        {
                                            strPropertyVa = pv_parameter;
                                        }
                                        if (string.IsNullOrEmpty(brandId))
                                        {
                                            propertyUrl = Common.UrlStr("productlist", "q_cid=" + productClassId + ";q_property=" + strPropertyVa + "", "product/");
                                        }
                                        else
                                        {
                                            propertyUrl = Common.UrlStr("productlist", "q_cid=" + productClassId + ";q_productbrand=" + brandId + ";q_property=" + strPropertyVa + "", "product/");
                                        }
                                        strPropertyValueList += "<span class='" + style + "'><a href='" + propertyUrl + "'>" + pv + "</a></span>";
                                    }
                                }
                                if (strContent.Contains("[#Name]"))
                                {
                                    strContent = strContent.Replace("[#Name]", "" + property.PropertyName + "");
                                }
                                if (strContent.Contains("[#Whole]"))
                                {
                                    string fullPropertyStyle = "property_Selected";
                                    
                                    string urls = Common.UrlStr("productlist", "q_cid=" + productClassId + "", "product/");
                                    if (!string.IsNullOrEmpty(brandId.Trim()))
                                    {
                                        urls = Common.UrlStr("productlist", "q_cid=" + productClassId + ";q_productbrand=" + brandId + "", "product/");
                                    }
                                    if (!string.IsNullOrEmpty(fullPropertyInfo))
                                    {
                                        urls = Common.UrlStr("productlist", "q_cid=" + productClassId + ";q_property=" + fullPropertyInfo + "", "product/");
                                        if (!string.IsNullOrEmpty(brandId.Trim()))
                                        {
                                            urls = Common.UrlStr("productlist", "q_cid=" + productClassId + ";q_productbrand=" + brandId + ";q_property=" + fullPropertyInfo + "", "product/");
                                        }
                                    }
                                    string[] fullPropertyInfoArr = strProperty.Split(',');
                                    if (fullPropertyInfoArr.Length > 0)
                                    {
                                        for (int i = 0; i < fullPropertyInfoArr.Length; i++)
                                        {
                                            string[] fullPropertyInfoOneArr = fullPropertyInfoArr[i].Split('_');
                                            if (fullPropertyInfoOneArr[0] == property.ID.ToString())
                                            {
                                                fullPropertyStyle = "property_UnSelected";
                                            }
                                        }
                                    }
                                    else
                                    {
                                        fullPropertyStyle = "property_UnSelected";
                                    }
                                    strContent = strContent.Replace("[#Whole]", "<span class='" + fullPropertyStyle + "'><a href='" + urls + "'>全部</a></span>");
                                }
                                if (strContent.Contains("[#Value]"))
                                {
                                    strContent = strContent.Replace("[#Value]", strPropertyValueList);
                                }

                            }
                            else
                            {
                                continue;
                            }
                            if (!string.IsNullOrEmpty(strContent))
                            {
                                rowsCount++;
                                shtml.Append(strContent);
                                if (proGroupRows==rowsCount)
                                {
                                    if (str_PropertyGroupState == "false")
                                    {
                                        break;
                                    }
                                    shtml.Append("<span  style=\"display:none\">");
                                }
                            }
                        }
                    }
                    if (rowsCount > proGroupRows)
                    {
                        shtml.Append("</span>");
                    }

                }
            }
            return shtml.ToString();
        }
    }
}
